using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AntidoteEngine
{
    class Layer
    {
        List<Sprite> LayerSprites;
        bool offset;
        bool hide;

        public int Count
        {
            get { return LayerSprites.Count; }
        }

        public void Add(Sprite newsprite)
        {
            LayerSprites.Add(newsprite);
        }

        public bool Offset
        {
            get { return offset; }
        }

        public bool Hide
        {
            get { return hide; }
            internal set { hide = value; }
        }

        public List<Sprite> GetList
        {
            get { return LayerSprites; }
        }

        public Sprite RemoveAt(int index)
        {
            Sprite returnsprite;
            if (index >= 0 && index < LayerSprites.Count)
            {
                returnsprite = LayerSprites[index];
                LayerSprites.RemoveAt(index);

                return returnsprite;
            }

            return null;
        }

        public Layer(bool off)
        {
            LayerSprites = new List<Sprite>();
            offset = off;
        }
    }

    public class SpriteRenderManager
    {
        List<Layer> SpriteLayers;
        SpriteBatch SpriteMngrBatch;
        
        public SpriteRenderManager()
        {
            SpriteLayers = new List<Layer>();
            SpriteMngrBatch = new SpriteBatch(GraphicsManager.GraphicsDevice);
        }

        public bool AddDrawSprite(int LayerNumber, Sprite newdrawsprite)
        {
            if (LayerNumber >= 0 && LayerNumber < SpriteLayers.Count)
            {
                SpriteLayers[LayerNumber].GetList.Add(newdrawsprite);
                return true;
            }

            return false;
        }

        public bool RemoveSprite(int LayerNumber, int index)
        {
            if (LayerNumber >= 0 && LayerNumber < SpriteLayers.Count)
            {
                if (index >= 0 && index < SpriteLayers[LayerNumber].Count)
                {
                    SpriteLayers[LayerNumber].GetList.RemoveAt(index);
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Make new layer
        /// </summary>
        /// <param name="offset">Will it be affected by the graphics manager offset</param>
        public void AddLayer(bool offset)
        {
            SpriteLayers.Add(new Layer(offset));
        }

        public bool RemoveLayer(int index)
        {
            if (index >= 0 && index < SpriteLayers.Count)
            {
                SpriteLayers.RemoveAt(index);
                return true;
            }

            return false;
        }

        public void HideLayer(int index)
        {
            if (index >= 0 && index < SpriteLayers.Count)
                SpriteLayers[index].Hide = true;
        }

        public void ShowLayer(int index)
        {
            if (index >= 0 && index < SpriteLayers.Count)
                SpriteLayers[index].Hide = false;
        }

        public void RenderSprites()
        {
            Vector2 Offset = GraphicsManager.SpriteOffset;
            SpriteMngrBatch.Begin();

            for (int l = 0; l < SpriteLayers.Count; l++)
            {
                if (!SpriteLayers[l].Hide)
                {
                    Vector2 offset;

                    if (SpriteLayers[l].Offset)
                        offset = Offset;
                    else
                        offset = Vector2.Zero;

                    for (int s = 0; s < SpriteLayers[l].Count; s++)
                    {
                        Sprite CurrentSprite = SpriteLayers[l].GetList[s];
                        if (!CurrentSprite.HideSprite)
                        {
                            Vector2 Position = CurrentSprite.Position;

                            CurrentSprite.Scale = GraphicsManager.SpriteScale;

                            SpriteMngrBatch.Draw(CurrentSprite.Image, CurrentSprite.Position,
                                                 CurrentSprite.SourceRectangle, CurrentSprite.SpriteColor,
                                                 CurrentSprite.Rotation, CurrentSprite.RotationOrigin,
                                                 CurrentSprite.Scale, CurrentSprite.SpriteEffect, 1f);
                        }
                    }
                }
            }
        }
    }
}
